package com.sugon.controller.device;

import cn.hutool.core.util.StrUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.dao.DeviceInfoDao;
import com.sugon.entity.DevAlarmBoolEntity;
import com.sugon.entity.DeviceInfoEntity;
import com.sugon.service.DevAlarmBoolService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 设备布尔类型报警码对应表Controller
 *
 * @author sugon
 * @date 2020-11-30 19:54:12
 */
@Controller
@RequestMapping("devalarmbool")
public class DevAlarmBoolController {
    @Autowired
    private DevAlarmBoolService devAlarmBoolService;
    @Autowired
    private DeviceInfoDao deviceInfoDao;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<DevAlarmBoolEntity> devAlarmBoolList = devAlarmBoolService.queryList(query);
        int total = devAlarmBoolService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(devAlarmBoolList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        DevAlarmBoolEntity devAlarmBool = devAlarmBoolService.queryObject(id);

        return R.ok().put("devAlarmBool", devAlarmBool);
    }

    /**
     * 保存
     */
    @AutoLog("设备布尔类型报警码对应表--保存")
    @RequestMapping("/save")
    @ResponseBody
    public R save(@RequestBody DevAlarmBoolEntity devAlarmBool) {
        String devLastCode = devAlarmBool.getDevLastCode();
        if (StrUtil.isEmpty(devLastCode)) {
            return R.error("设备底层码必填！");
        }
        DeviceInfoEntity deviceInfo = deviceInfoDao.getInfoByDevLastCode(devLastCode);
        if (deviceInfo == null) {
            return R.error("非法低层设备码！");
        }
        devAlarmBool.setDevId(deviceInfo.getId());
        return devAlarmBoolService.save(devAlarmBool) > 0 ? R.ok() : R.error();
    }

    /**
     * 修改
     */
    @AutoLog("设备布尔类型报警码对应表--修改")
    @RequestMapping("/update")
    @ResponseBody
    public R update(@RequestBody DevAlarmBoolEntity devAlarmBool) {
        String devLastCode = devAlarmBool.getDevLastCode();
        if (StrUtil.isEmpty(devLastCode)) {
            return R.error("设备底层码必填！");
        }
        DeviceInfoEntity deviceInfo = deviceInfoDao.getInfoByDevLastCode(devLastCode);
        if (deviceInfo == null) {
            return R.error("非法低层设备码！");
        }
        devAlarmBool.setDevId(deviceInfo.getId());
        return devAlarmBoolService.update(devAlarmBool) > 0 ? R.ok() : R.error();
    }

    /**
     * 删除
     */
    @AutoLog("设备布尔类型报警码对应表--删除")
    @RequestMapping("/delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        devAlarmBoolService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<DevAlarmBoolEntity> list = devAlarmBoolService.queryList(params);

        return R.ok().put("list", list);
    }

    @RequestMapping("/imports")
    @ResponseBody
    public R imports(@RequestParam("file") MultipartFile file) throws Exception {
        int rows = devAlarmBoolService.imports(file);
        return R.ok("成功导入" + rows + "记录");
    }
}
